package com.kai.service.message.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.kai.service.data.request.message.MessageBoardRequest;
import com.kai.service.data.request.message.PagingMessageBoardRequest;
import com.kai.service.data.response.message.MessageBoardResponse;
import com.kai.service.data.response.user.UserInfoResponse;
import com.kai.service.enums.ResultCodeEnum;
import com.kai.service.exception.GenericException;
import com.kai.service.message.entity.MessageBoard;
import com.kai.service.message.mapper.MessageBoardMapper;
import com.kai.service.message.service.IMessageBoardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kai.service.user.entity.User;
import com.kai.service.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 留言板 服务实现类
 * </p>
 *
 * @author wjk
 * @since 2022-04-19
 */
@Service
public class MessageBoardServiceImpl extends ServiceImpl<MessageBoardMapper, MessageBoard> implements IMessageBoardService {

    @Autowired
    private IUserService userService;

    /**
     * 添加留言
     *
     * @param messageBoardRequest
     * @return
     */
    @Override
    public MessageBoardResponse createMessage(MessageBoardRequest messageBoardRequest) {
        MessageBoard messageBoard = new MessageBoard();
        BeanUtils.copyProperties(messageBoardRequest, messageBoard);
        messageBoard.setCreateDatetime(new Date());

        this.save(messageBoard);

        UserInfoResponse userInfo = userService.getUserInfo(messageBoard.getCommenterId());
        MessageBoardResponse messageBoardResponse = new MessageBoardResponse();
        BeanUtils.copyProperties(messageBoard, messageBoardResponse);
        messageBoardResponse.setUserInfo(userInfo);

        return messageBoardResponse;
    }

    /**
     * 分页查询留言
     *
     * @param pagingMessageBoardRequest
     * @return
     */
    @Override
    public IPage<MessageBoardResponse> pagingMessage(PagingMessageBoardRequest pagingMessageBoardRequest) {
        IPage<MessageBoard> messageBoardIPage = this.lambdaQuery()
                .ne(!Objects.equals(pagingMessageBoardRequest.getUserId(), 0L), MessageBoard::getCommenterId, 0)
                .orderByDesc(MessageBoard::getCreateDatetime)
                .page(new Page<>(pagingMessageBoardRequest.getPageIndex(), pagingMessageBoardRequest.getPageSize()));

        List<MessageBoardResponse> messageBoardResponseList = Lists.newArrayList();
        List<MessageBoard> messageBoards = messageBoardIPage.getRecords();
        if (!CollectionUtils.isEmpty(messageBoards)) {
            Set<Long> commenterIds = messageBoards.stream().map(MessageBoard::getCommenterId).collect(Collectors.toSet());
            List<User> users = userService.lambdaQuery().in(User::getId, commenterIds).list();
            Map<Long, UserInfoResponse> userInfoResponseMap = users.stream().map(UserInfoResponse::new).collect(Collectors.toMap(UserInfoResponse::getId, userInfoResponse -> userInfoResponse));

            messageBoardResponseList = messageBoards.stream().map(messageBoard -> {
                MessageBoardResponse messageBoardResponse = new MessageBoardResponse();
                BeanUtils.copyProperties(messageBoard, messageBoardResponse);
                messageBoardResponse.setUserInfo(userInfoResponseMap.get(messageBoard.getCommenterId()));

                return messageBoardResponse;
            }).collect(Collectors.toList());
        }

        return new Page<MessageBoardResponse>()
                .setTotal(messageBoardIPage.getTotal())
                .setPages(messageBoardIPage.getPages())
                .setCurrent(messageBoardIPage.getCurrent())
                .setSize(messageBoardIPage.getSize())
                .setRecords(messageBoardResponseList);
    }

    /**
     * 修改留言
     *
     * @param messageId
     * @param messageBoardRequest
     * @return
     */
    @Override
    public Boolean updateMessage(Integer messageId, MessageBoardRequest messageBoardRequest) {
        MessageBoard messageBoard = this.getById(messageId);
        if (Objects.isNull(messageBoard)){
            throw new GenericException(ResultCodeEnum.MESSAGE_NOT_EXIST);
        }
        return messageBoard.setContent(messageBoard.getContent()).updateById();
    }

    /**
     * 删除留言
     *
     * @param messageId
     * @return
     */
    @Override
    public Boolean deleteMessage(Integer messageId) {
        return this.removeById(messageId);
    }
}
